using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 基础数据管理
    /// </summary>
    public class BaseDataService : IBaseDataService
    {
        private readonly IProvinceRepository _provinceRepository;
        private readonly ICityRepository _cityRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public BaseDataService(
            IProvinceRepository provinceRepository,
            ICityRepository cityRepository)
        {
            _provinceRepository = provinceRepository;
            _cityRepository = cityRepository;
        }

        /// <summary>
        /// 省份列表
        /// </summary>
        public async Task<List<BaseDataDto>> GetProvinceListAsync()
        {
            var result = await _provinceRepository.AsNoTracking().Select(m =>
            new BaseDataDto
            {
                ID = m.ID,
                Name = m.P_Name,
                FullName = m.P_FullName,
                Code = m.P_Code
            }).ToListAsync();
            return result;
        }

        /// <summary>
        /// 城市列表
        /// </summary>
        /// <param name="provinceId">省份ID</param>
        public async Task<List<BaseDataDto>> GetCityListAsync(Guid? provinceId)
        {
            var queryable = _cityRepository.AsNoTracking();
            if (provinceId.HasValue)
            {
                queryable = queryable.Where(m => m.ProvinceID == provinceId.Value);
            }

            var result = await queryable.Select(m =>
             new BaseDataDto
             {
                 ID = m.ID,
                 Name = m.C_Name,
                 FullName = m.C_FullName,
                 Code = m.C_Code,
                 ParentId = m.ProvinceID
             }).ToListAsync();
            return result;
        }
    }
}
